home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 471 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.3 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: The realloc question: rationale?
  5. Date: 23 Feb 1996 13:23:11 PST
  6. Organization: Fakultdt f|r Mathematik und Informatik
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4gl9bt$r31@news.BelWue.DE>
  9. References: <4g903m$7g8@mari.onr.com>
  10. Reply-To: dietmar.kuehl@uni-konstanz.de
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 23 Feb 1996 20:51:09 GMT
  13. X-Newsreader: TIN [version 1.2 PL2]
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMS4waEy4NqrwXLNJAQHOsAH7Bs1e1Zm+TPHC0XObsjIswKfWtOjKAAog
  16.     XtLJrfztzs5BGxC+uprxGizSJdAqmMX92J4XLAnT3rwUP21RZMgNOA==
  17.     =9HYk
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. Hi,
  21.  
  22. Kerry Kimbrough (kk@onr.com) wrote:
  23. : I gather from a recent message here that ANSI C++ (still) does not
  24. : define the moral equivalent of realloc(). This has always seemed
  25. : regrettable to me. I also find the rationale expressed in the 
  26. : comp.lang.c++ FAQ to be completely unsatisfactory. Surely someone
  27. : from this group can tell me why this omission is either a good thing
  28. : or at least not a major obstacle for efficient implementation of 
  29. : extensible arrays.
  30.  
  31. I can't see why it is a good thing (neither can I see why it is a bad
  32. thing...) but I can tell you why it is no major obstacle: It is
  33. possible to
  34.  
  35.   - allocate "raw" (i.e. uninitialized memory) with 'operator
  36.     new(size_t)' or relatives. Correspondingly, it is possible to
  37.     release "raw" memory (i.e. memory where the objects contained were
  38.     destructed) using 'operator delete(void*)' or relatives.
  39.   - construct an object in-place using 'operator new(size_t, void*)'
  40.     (placement new).
  41.   - destruct a specific object of type 'T' using an explicit destructor
  42.     call ('~T()').
  43.  
  44. These are the major ingredients necessary for a 'realloc()' function.
  45. The only missing aspect is the possibility, to make use of the
  46. knowledge of the memory system about the size of the memory block
  47. occupied of the array to be resized. This is unfortunate but a
  48. specialized array class can even circumvent this problem by using a
  49. specialized memory management facility. I think an extension of the
  50. language in this direction is neither necessary nor suitable (and maybe
  51. even impossible for some environments).
  52.  
  53. The basic operation of a 'realloc()' function increasing the size of an
  54. array would be to
  55.  
  56.   - allocate raw memory
  57.   - copy the elements from the orginal array using placement new
  58.   - construct the remaining objects
  59.   - destruct the elements in the original array
  60.   - release the the original array as raw memory
  61.   - return a pointer to the newly allocated array
  62.  
  63. (An implementation without error checking of the procedure is not much
  64. longer than this description. See e.g. the article "How to 'renew' a
  65. built-in array" in comp.lang.c++.moderated or my implementation of an
  66. array class at
  67. <http://www.informatik.uni-konstanz.de/~kuehl/c++/array.h.html>).
  68.  
  69. Although this procedure CANNOT be used for arrays allocated with 'new
  70. T[size]' (e.g. because the implementation can place auxiliary data in
  71. front of the array such that the address to be passed to 'operator
  72. delete[]()' cannot be figured out) it CAN be used inside an array class
  73. where the built-in array used for the representation is created like
  74. the returned array in the 'realloc()' function (of course it is also
  75. impossible to release such an array with 'delete[]...': It is also
  76. necessary to do this by hand).
  77.  
  78. Note, that this implementation even provides some additional freedom: A
  79. built-in version of 'realloc()' can only use the copy constructor to
  80. initialize the new array. A "home-grown" version can use a "move
  81. constructor", i.e. a constructor which transfers allocated resources to
  82. the new array leaving the original objects in a state only valid for
  83. destruction (destruction is exactly what happens next to the orginal
  84. objects).
  85. --
  86. dietmar.kuehl@uni-konstanz.de
  87. http://www.informatik.uni-konstanz.de/~kuehl
  88. I am a realistic optimist - that's why I appear to be slightly pessimistic
  89. ---
  90. [ To submit articles: Try just posting with your newsreader.  If that fails,
  91.                       use mailto:std-c++@ncar.ucar.edu
  92.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  93.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  94.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  95. ]
  96.